package com.lzwork.gpt.utility.caller;

import com.alibaba.fastjson.JSONObject;
import com.lzwork.utility.TimeTicker;
import com.lzwork.utility.str.StrUtility;
import com.lzwork.web.HttpRequester.ReturnData;
import com.lzwork.web.RemoteCaller.CallResult;
import com.lzwork.web.WebResultCode;

/**
 * @author LiZheng
 * @date 2023年9月1日 下午2:53:52
 */

public class GptCallResult extends CallResult
{
	String response = "";
	String gpuCost = "";

	public String getResponse()
	{
		return response;
	}

	public void setResponse(String response)
	{
		this.response = response;
	}

	public String getGpuCost()
	{
		return gpuCost;
	}

	public void setGpuCost(String gpuCost)
	{
		this.gpuCost = gpuCost;

	}

	@Override
	public void addErrInfo(String err)
	{
		super.addErrInfo(err);

		if (err.contains("CUDA out of memory"))
		{
			setErrType("GpuOOM");
		}
	}

	@Override
	protected void afterCall(ReturnData responseData)
	{
		super.afterCall(responseData);

		JSONObject obj = getReturnData();
		if (obj != null)
		{
			setResponse(obj.getString("response"));

			gpuCost = obj.getString("cost");
			if (!StrUtility.stringValid(gpuCost))
				gpuCost = TimeTicker.formatTimeStr(getTimeCost(), null);
		}
	}

	public JSONObject getError()
	{
		String err = getErrMessage();

		return StrUtility.stringValid(err) ? WebResultCode.GPU_CALL_ERROR.toErrJobj(err) : null;
	}
}
